package com.meme.editor.gui.sprite2d;

import com.meme.editor.EntityObject;
import com.meme.editor.event.EntityObjectListener;
import com.meme.editor.gui.EntityChildrenListView;
import com.meme.editor.gui.PropertyEditPanel;
import com.meme.editor.model.sprite2d.Module;
import com.meme.editor.model.sprite2d.ModuleImage;
import com.meme.editor.model.sprite2d.ModuleGroup;
import com.meme.editor.model.sprite2d.ModulePart;
import com.meme.editor.model.sprite2d.PureModuleGroup;

import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;


import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.JScrollPane;

import org.apache.log4j.Logger;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

public class ModuleGroupPanel extends JPanel
{
	static Logger logger = Logger.getLogger (ModuleGroupPanel.class);
	
	public final static String ViewMode_Bird = "bird";
	public final static String ViewMode_Icons = "icons";
	public final static String ViewMode_List = "list";
	
	public final static int EditMode_ImageModule = 0;
	public final static int EditMode_CompositedModule = 1;
	public final static int EditMode_AnimModule = 2;
	
	ModuleGroup mModuleGroup;
	
	String mViewMode; // edit, icon, list
	int    mEditMode;
	JComponent mRealView;
	
	public ModuleGroupPanel (ModuleGroup moduleGroup, String viewMode, int editMode, boolean showToolbar)
	{
		mModuleGroup = moduleGroup;
		
		setLayout ( new BorderLayout() );
		
		if ( showToolbar )
			createToolbarPanel ();
		
		mEditMode = editMode;

		setViewMode (viewMode);
	}
	
	EntityChildrenListView mChildrenListView;
//	PropertyEditPanel      mChildPropertyView;
	
	public void setChildrenListView (EntityChildrenListView childrenView)
	{
		mChildrenListView = childrenView;
		
		if ( mChildrenListView != null && mRealView != null && mRealView instanceof ModuleGroupView_Bird )
		{
			( (ModuleGroupView_Bird) mRealView ).setChildrenListView (mChildrenListView);
		}
	}
	
//	public void setChildPropertyView (PropertyEditPanel propertyView)
//	{
//		mChildPropertyView = propertyView;
//	}
	
	public void setViewMode (String mode)
	{
		if ( mViewMode != null && mViewMode.equals (mode) )
			return;
		
		JComponent panel;
		
		if ( mRealView != null )
		{
			if ( mRealView instanceof ModuleGroupView_Bird)
				remove(mRealView);
			else
				remove(mRealView.getParent().getParent()); // the JScrollPane
		}
		
		if ( mode.equals(ViewMode_Bird) )
		{
			mViewMode = ViewMode_Bird;
			
			mRealView = new ModuleGroupView_Bird ((PureModuleGroup)mModuleGroup); 
			( (ModuleGroupView_Bird) mRealView ).setReadOnly (mEditMode != EditMode_ImageModule);
			setChildrenListView (mChildrenListView);
			
			panel = mRealView;
		}
		else  if ( mode.equals(ViewMode_Icons) )
		{
			mViewMode = ViewMode_Icons;
			mRealView = new ModuleGroupView_Icons (mModuleGroup);
			
			
			panel = new JScrollPane ( mRealView );
		}
//		else // if ( mode.equals(ViewMode_List) )
//		{
//			mViewMode = ViewMode_List;
//			
//			mRealView = new EntityChildrenView (mImageModuleGroup); 
//		}
		else
		{
			logger.error ("Unkonwn mode: " + mode);
			
			return;
		}
		
		
		
		add (panel, BorderLayout.CENTER);
		updateUI();
		//this.doLayout ();
	}
	
	void createToolbarPanel ()
	{
		JToolBar toolbar = new JToolBar ();
		add (toolbar, BorderLayout.SOUTH);
		
		toolbar.setBorderPainted ( true );
		toolbar.setRollover ( true );
		toolbar.setFloatable ( false );
		
		toolbar.add (new SwitchToIconsView ());
		toolbar.add (new SwitchToBirdView ());
		toolbar.addSeparator();
	}
	
	class SwitchToIconsView extends AbstractAction 
	{
		public SwitchToIconsView ()
		{
			super ("Icons View");
		}
		
	    public void actionPerformed(ActionEvent e) 
	    {
	    	setViewMode (ViewMode_Icons);
	    }
	}
	
	class SwitchToBirdView extends AbstractAction 
	{
		public SwitchToBirdView ()
		{
			super ("Bird View");
		}
		
	    public void actionPerformed(ActionEvent e) 
	    {
	    	setViewMode (ViewMode_Bird);
	    }
	}
}
